<?php
class Application_Service_Winner
{
	private static $_instance = NULL;
	
    private function __construct () {}
    
    private function __clone() {}
    
    public static function getInstance() {
    	if(self::$_instance == NULL) {
    		self::$_instance = new self();
    	}
    	return self::$_instance;
    }
    
    /**
     * 
     * 获得最近中拍信息
     * @param int $count
     */
    public function getRecentWinner($count) {
    	
    	$winnerModel = new Application_Model_Winner();
    	$winners = $winnerModel->getRecentWinner($count);
    	
    	$result = array();
    	
    	foreach ($winners as $winner) {
    		$winner = $this->convertWinner($winner);
    		array_push($result, $winner);
    	}
    	
    	return $result;
    }
    
    /**
     * 
     * 获得中拍总数
     */
    public function getWinnerNum() {
    	
    	$winnerModel = new Application_Model_Winner();
    	
		$temp = $winnerModel->getWinnerNum(); // 结果为array()类型
		$winnerNum = $temp[0];
		
		return $winnerNum;
    	
    }
    
    /**
     * 
     * 获得中拍信息
     * @param int $offset
     */
    public function getWinner($page) {
    	
    	$count = Application_Model_Constants::$WINNER_PAGE_NUM;
    	
    	$winnerModel = new Application_Model_Winner();
    	$winners = $winnerModel->getWinner($count, $page);
    	
    	$result = array();
    	
    	foreach ($winners as $winner) {
    		$winner = $this->convertWinner($winner);
    		array_push($result, $winner);
    	}
    	
    	return $result;
    }
    
    private function convertWinner($winner) {
    	
    	$user = $winner->findParentRow("Application_Model_User");
    	$auction = $winner->findParentRow("Application_Model_Auction");
    	
    	$winner = $winner->toArray();
    	
    	// 获得中拍商品，中拍人信息
    	$winner['user_name'] = $user->name;
    	$winner['user_home_page'] = $user->home_page;
    	$winner['auction_id'] = $auction->id;
    	$winner['auction_name'] = $auction->name;
    	$winner['auction_image1'] = $auction->image1;
    	
    	return $winner;
    }
}
?>